package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.vo.StatVO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.BedManagmentService;
import com.example.mybatisplus.model.domain.BedManagment;

import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author ablbb
 * @since 2024-07-18
 * @version v1.0
 */
@RestController
@RequestMapping("/api/bedManagment")
public class BedManagmentController {
    @Autowired
    private BedManagmentService bedManagmentService;

    @GetMapping("pageList")
    public JsonResponse pageList(BedManagment bedManagment, PageDTO dto){
        Page<BedManagment> page = bedManagmentService.pageList(bedManagment, dto);
        return JsonResponse.success(page);
    }

    // 修改用户
    @PostMapping("edit")
    public JsonResponse edit(@RequestBody BedManagment bedManagment){
        boolean b =bedManagmentService.updateById(bedManagment);
        return JsonResponse.success(b);
    }

    // 删除用户
    @PostMapping("removeById2")
    public JsonResponse removeById2(@RequestBody BedManagment bedManagment){
        // 返回值表示是否删除成功
        boolean b = bedManagmentService.removeById(bedManagment.getRoomId());
        // res.data
        return JsonResponse.success(b);
    }

    // 添加单个用户
    @PostMapping("addById")
    public JsonResponse addById(@RequestBody BedManagment bedManagment) {
        boolean a = bedManagmentService.saveOrUpdate(bedManagment);
        return JsonResponse.success(a);
    }

    @PostMapping("/generateRooms")
    public ResponseEntity<String> generateRooms(@RequestBody Map<String, Object> params) {
        String buildingId = (String) params.get("buildingId");
        int floor = Integer.parseInt((String) params.get("floor"));
        int roomNumber = Integer.parseInt((String) params.get("roomNumber"));

        BedManagment bedManagment = new BedManagment();
        bedManagment.setBuildingId(buildingId);
        bedManagment.setFloor(floor);
        bedManagment.setRoomNumber(roomNumber);
        bedManagment.setDeleted(false);
        bedManagment.setEnabled(true);

        bedManagmentService.generateRooms(buildingId, floor, roomNumber);

        return ResponseEntity.ok("房间数据生成成功");
    }


    // 统计不同年级的学位（默认A是本科生，B是研究生），均为可用床位
    @GetMapping("tongjibed")
    public JsonResponse tongjibed(){
        List<StatVO> list=bedManagmentService.statbed();
        return JsonResponse.success(list);
    }

    /**
    * 描述：根据Id 查询
    *
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        BedManagment  bedManagment =  bedManagmentService.getById(id);
        return JsonResponse.success(bedManagment);
    }

    *
    * 描述：根据Id删除
    *

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        bedManagmentService.removeById(id);
        return JsonResponse.success(null);
    }


    *
    * 描述：根据Id 更新
    *

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateBedManagment(BedManagment  bedManagment) throws Exception {
        bedManagmentService.updateById(bedManagment);
        return JsonResponse.success(null);
    }


    *
    * 描述:创建BedManagment
    *

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(BedManagment  bedManagment) throws Exception {
        bedManagmentService.save(bedManagment);
        return JsonResponse.success(null);
    }*/
}

